package com.neo.demo2.web;

import com.neo.demo2.domain.Category;
import com.neo.demo2.mapper.CategoryMapper;
import com.neo.demo2.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author: chenb
 * @CreateTime: 2024/3/20 13:42
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/category")
public class CategoryController {
  //在CategoryServiceImpl定义了@Service实现类所以可以得到默认首字母小写的对象
  @Autowired
  private CategoryService categoryService;

  @Autowired
  private CategoryMapper categoryMapper;

  /**
   * 功能描述: Category 保存接口
   */
  @GetMapping("add")
  public Object add(String name,String ext1,String ext2,String ext3){
    Category category = new Category();
    category.setCategoryName(name);
    category.setExt1(ext1);
    category.setExt2(ext2);
    category.setExt3(ext3);
    category.setCreateTime(new Date());
    category.setIsDel("0");
    int id = categoryService.add(category);
    return id;
  }

  /**
   * 功能描述：查找全部
   * 这里和下面是直接调用跳过Servise层，直接到DAO层
   */
  @GetMapping("findAll")
  public Object findAll(){
    return categoryMapper.getAll();
  }

  /**
   * 查找单个
   */
  @GetMapping("find_by_id")
  public Object findById(long id){
    return categoryMapper.findById( id);
  }

  /**
   * 删除单个
   */
  @GetMapping("del_by_id")
  public Object delById(int id){
    Category category = new Category();
    category.setIsDel("1");
    category.setId(id);
//    categoryMapper.delete(id);
    categoryMapper.updatedel(category);
    return "";
  }

  /**
   *更新
   */
  @GetMapping("update")
  public Object update(String name,int id){
    Category category = new Category();
    category.setCategoryName(name);
    category.setId(id);
    categoryMapper.update(category);
    return "";
  }

}
